
public class Test {
    public static void main(String[] args) throws Exception {
        ShapeFactory f = Math.random() > 0.5 ? new CricleFactory() : new RectangleFactory();
        Shape s = f.make();
        s.draw();
    }
}

// 定义一个用于创建对象的接口，让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类。
// 工厂方法即Factory Method，是一种对象创建型模式。
// 工厂方法的目的是使得创建对象和使用对象是分离的，并且客户端总是引用抽象工厂和抽象产品
interface Shape {
    void draw();
}

class Cricle implements Shape {
    @Override
    public void draw() {
        System.out.println("Cricle draw is running");
    }
}

class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Rectangle draw is running");
    }
}

abstract class ShapeFactory {
    abstract Shape createShape();

    final Shape make() {
        Shape s = this.createShape();
        return s;
    }
}

class CricleFactory extends ShapeFactory {
    @Override
    public Shape createShape() {
        return new Cricle();
    }
}

class RectangleFactory extends ShapeFactory {
    @Override
    public Shape createShape() {
        return new Rectangle();
    }
}